
var showBtn = $('#showPassword');
var passWordInput = $('#inputPassword');
var submitHead=document.getElementById('submitHead');
var oHeadFile=document.getElementById('setHeadFile');
var oSetHeadimg=document.getElementById('setHeadimg');
var getRootData=JSON.parse(localStorage.getItem('rootdata'))||[
    { name:'龙龙',root:'高级权限',username:'L020050612',password:'123456',index:0},
    { name:'泽泽',root:'普通权限',username:'Z020070414',password:'123456',index:1},
    { name:'军军',root:'高级权限',username:'J020020512',password:'123456',index:2},
    { name:'世祥',root:'高级权限',username:'S020070424',password:'123456',index:3},
    { name:'涵涵',root:'高级权限',username:'H020170812',password:'123456',index:4},
    { name:'锦涵',root:'高级权限',username:'J020140917',password:'123456',index:5},
    { name:'维强',root:'普通权限',username:'W020151112',password:'123456',index:6},
];
var getThisSrc=JSON.parse(localStorage.getItem('login'))[0].headsrc;
var getI;
$('#headimg').attr('src',getThisSrc||'../images/hjh-img/user.jpg');
$('#smalhead').attr('src',getThisSrc||'../images/hjh-img/user.jpg');
showBtn.click(
    function () {

        if (passWordInput.attr('type') == 'text') {
            showBtn.attr('class', 'glyphicon glyphicon-eye-open');
            passWordInput.attr('type', 'password');
        } else {
            passWordInput.attr('type', 'text');
            showBtn.attr('class', 'glyphicon glyphicon-eye-close');
        }
        return false
    }
);
var changeFlage=false;
submitHead.onclick=function () {
    console.log(oHeadFile.files[0]);
    var filereader= new FileReader();
    filereader.readAsDataURL(oHeadFile.files[0]);

    filereader.onloadend=function (e) {
        var src=e.target.result;
        console.log(src);

        changeFlage=true;

        for (var i=0;i<getRootData.length;i++){
            if (getRootData[i].index==getLogin[0].index){
                getI=i;
                getRootData[i].headsrc=src;
                localStorage.setItem('rootdata',JSON.stringify(getRootData));
                oSetHeadimg.src=getRootData[i].headsrc;
                localStorage.setItem('login',JSON.stringify([getRootData[i]]))

            }

        }
    }

};
var changeHead=$('#changeHead')
changeHead.click(function () {

    if (changeFlage){


        $('#headimg').attr('src',getRootData[getI].headsrc);
        $('#smalhead').attr('src',getRootData[getI].headsrc);
        myAlert('更改成功');
    }else {
        myAlert('您没有上传图片')
    }



});
function myAlert(setVal) {

    var getMyalert=$('#myAlertEle');
    getMyalert.html(setVal);
    getMyalert.css(
        {
            display:'block',

        }
    );
    setTimeout(function () {
        getMyalert.css(
            {
                top:'20%',
                opacity:'1'
            }
        )
    },100);
    setTimeout(
        function () {
            getMyalert.css(
                {
                    top:'0%',
                    opacity:'0'
                }
            )
        },1000
    );
    setTimeout(
        function () {
            getMyalert.css(
                {
                    display:'none'
                }
            )
        },1300
    )


}
var getLogin=JSON.parse(localStorage.getItem('login'));
$('#nameInput').attr('placeholder',getLogin[0].name);
$('#usernameInput').attr('placeholder',getLogin[0].username);
$('#setName').click(function () {

    if ($('#nameInput').val().trim()){
        var getIndex=JSON.parse(localStorage.getItem('login'))[0].index;

        for (var i=0;i<getRootData.length;i++){
            if (getRootData[i].index==getIndex){
                getRootData[i].name=$('#nameInput').val();
                localStorage.setItem('rootdata',JSON.stringify(getRootData));
                localStorage.setItem('login',JSON.stringify([getRootData[i]]))
                $('#nameInput').attr('placeholder',getRootData[i].name);
                $('#homeUserName').text(getRootData[i].name)
            }
        }
        myAlert('更改成功')
    }else {

        myAlert('不能为空')

    }



});
$('#setUserName').click(function () {
    if ($('#usernameInput').val().trim()){
        var getIndex=JSON.parse(localStorage.getItem('login'))[0].index;

        for (var i=0;i<getRootData.length;i++){
            if (getRootData[i].index==getIndex){
                getRootData[i].username=$('#usernameInput').val();
                localStorage.setItem('rootdata',JSON.stringify(getRootData));
                localStorage.setItem('login',JSON.stringify([getRootData[i]]))
                $('#usernameInput').attr('placeholder',getRootData[i].username);

            }
        }
        myAlert('更改成功')
    }else {

        myAlert('不能为空')

    }
})
$('#setPassword').click(function () {
    if ($('#inputPassword').val().trim()){
        var getIndex=JSON.parse(localStorage.getItem('login'))[0].index;

        for (var i=0;i<getRootData.length;i++){
            if (getRootData[i].index==getIndex){
                getRootData[i].password=$('#inputPassword').val();
                localStorage.setItem('rootdata',JSON.stringify(getRootData));
                localStorage.setItem('login',JSON.stringify([getRootData[i]]))
                $('#inputPassword').attr('placeholder',getRootData[i].password);

            }
        }
        myAlert('更改成功')
    }else {

        myAlert('不能为空')

    }
})

